package start.log;

import com.google.common.collect.Lists;

import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.util.*;

public class ObjectFieldExplorer {
    public static Map<String, Object> exploreObject(Object obj) {
        Map<String, Object> objectMap = new LinkedHashMap<>();
        exploreObject(obj, "", objectMap);
        return objectMap;
    }

    private static void exploreObject(Object obj, String prefix, Map<String, Object> objectMap) {
        if (obj == null) {
            return;
        }

        Class<?> objClass = obj.getClass();
        Field[] fields = objClass.getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            String fieldName = field.getName();
            Class<?> type = field.getType();
            System.out.println(type);
            Object fieldValue = null;
            try {
                fieldValue = field.get(obj);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }

            String fullFieldName = prefix.isEmpty() ? fieldName : prefix + "." + fieldName;
            if (fieldValue != null) {
                if (field.getType().isArray()) {
                    if (fieldValue.getClass().getComponentType().isPrimitive()) {
                        objectMap.put(fullFieldName, Arrays.toString((Object[]) fieldValue));
                    } else {
                        for (int i = 0; i < ((Object[]) fieldValue).length; i++) {
                            exploreObject(Array.get(fieldValue, i), fullFieldName + "[" + i + "]", objectMap);
                        }
                    }
                } else if (fieldValue instanceof Collection) {
                    Collection<?> collection = (Collection<?>) fieldValue;
                    int i = 0;
                    for (Object collectionElement : collection) {
                        exploreObject(collectionElement, fullFieldName + "[" + i + "]", objectMap);
                        i++;
                    }
                } else if (!field.getType().isPrimitive() && !field.getType().getName().startsWith("java.lang")) {
                    exploreObject(fieldValue, fullFieldName, objectMap);
                } else {
                    objectMap.put(fullFieldName, fieldValue);
                }
            }
        }
    }

    public static void main(String[] args) {
        // 创建一个示例对象，可以替换成你的对象
        SubsidyRateLog subsidyRateLog = new SubsidyRateLog();
        Address address = new Address();
        address.setCity("北京");
        address.setProvince("北京");
        AddressSub addressSub = new AddressSub();
        addressSub.setCode("001");
        address.setAddressSub(addressSub);
        subsidyRateLog.setName("北京");
        subsidyRateLog.setAddress(Lists.newArrayList(address));


        // 调用 exploreObject 方法遍历对象的字段并返回Map
        Map<String, Object> objectMap = exploreObject(subsidyRateLog);

        // 打印Map
        objectMap.forEach((key, value) -> System.out.println(key + ": " + value));
    }
}
